package com.gome.spiderkit.task.base;

import org.apache.log4j.Logger;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.Trigger;
import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory;


public class QuartzManager {

    private static Logger logger=Logger.getLogger(QuartzManager.class);
    private static SchedulerFactory sf = new StdSchedulerFactory();

    public static void addJob(String jobName, JobDetail job, Trigger trigger)
            throws SchedulerException {

        Scheduler sched = sf.getScheduler();
        
        sched.scheduleJob(job, trigger);
        
        if (!sched.isShutdown())
            sched.start();
    }
    
    public static void removeJob(String jobName, String jobGroupName,
            String triggerName, String triggerGroupName)
            throws SchedulerException {
        Scheduler sched = sf.getScheduler();
        TriggerKey tk = new TriggerKey(triggerName, triggerGroupName);
        JobKey jk = new JobKey(jobName, jobGroupName);
        sched.pauseTrigger(tk);
        sched.unscheduleJob(tk);
        sched.deleteJob(jk);
    }

}
